package hutool.http;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.thread.GlobalThreadPool;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;

/**
 * @author 黄乾坤
 * @create 2023-09-14 16:30
 */
public class DownloadFile {

    public static void main(String[] args) {

        HttpResponse response = HttpRequest.get("https://img0.baidu.com/it/u=2289446283,2987162055&fm=253&fmt=auto&app=120&f=JPEG?w=1422&h=800")
                                          .execute(false);

        ThreadUtil.execute(() -> {
            if (response.isOk()) {
                InputStream inputStream = response.bodyStream();
                try {
                    FileOutputStream fileOutputStream = new FileOutputStream(new File("C:\\Users\\12749\\Desktop\\1.png"));
                    IoUtil.copy(inputStream, fileOutputStream);
                    IoUtil.close(inputStream);
                    IoUtil.close(fileOutputStream);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
            response.close();
            System.out.println(22222);
        });

        GlobalThreadPool.shutdown(false);
        System.out.println(1111);
    }

}
